package cn.itsource.smartcampus.service.impl;

import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.mapper.TeacherMapper;
import cn.itsource.smartcampus.service.TeacherService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return teacherMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public AjaxResult<Integer> insert(Teacher tea) {
        Integer i = teacherMapper.insert(tea);
        return AjaxResult.ok(i);
    }

    @Override
    public int insertSelective(Teacher tea) {
        return 0;
    }

    @Override
    public Teacher selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Teacher tea) {
        return teacherMapper.updateByPrimaryKeySelective(tea);
    }

    @Override
    public List<Teacher> getAll() {
        return teacherMapper.getAll();
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            // 1.使用工具类从复杂文件中获取到老师信息集合
            List<Teacher> teachers = ExcelUtil.importExcel(file, Teacher.class);
            // 2.调用批量新增方法，保存所有老师信息
            teacherMapper.batchAdd(teachers);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
